﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class TestSetCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sOp = Request.QueryString["Op"].ToString();
                int iId = -1;

                if (sOp == "Edit")
                {
                    lblParent.Text = "Edit Test Set Details";
                    iId = int.Parse(Request.QueryString["ID"].ToString());
                }
                else
                {
                    lblParent.Text = "Add Test Set";
                }

                // first get the valid values for the drop-downs
                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {

                    {
                        DataTable dtList = objATBDB.GetDefTestSetColExecutionOrderValuesList();
                        dropdownExecutionOrder.DataValueField = "ID";
                        dropdownExecutionOrder.DataTextField = "Value";
                        dropdownExecutionOrder.DataSource = dtList;
                        dropdownExecutionOrder.DataBind();
                        dropdownExecutionOrder.Items.Add(new ListItem("[ Select ]", "0"));
                        dropdownExecutionOrder.SelectedValue = "0";
                    }
                    {
                        DataTable dtList = objATBDB.GetDefTestSetColMachineOpValuesList();
                        dropdownMachineOp.DataValueField = "ID";
                        dropdownMachineOp.DataTextField = "Value";
                        dropdownMachineOp.DataSource = dtList;
                        dropdownMachineOp.DataBind();
                        dropdownMachineOp.Items.Add(new ListItem("[ Select ]", "0"));
                        dropdownMachineOp.SelectedValue = "0";
                    }
                }

                if (sOp == "Edit")
                {
                    using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                    {
                        DataTable dtItemValues = objATBDB.GetDefTestSetRawDetails(iId);
                        txtName.Text = dtItemValues.Rows[0]["Name"].ToString();
                        txtDescription.Text = dtItemValues.Rows[0]["Description"].ToString();
                        dropdownExecutionOrder.SelectedValue = dtItemValues.Rows[0]["ExecutionOrder"].ToString();
                        dropdownMachineOp.SelectedValue = dtItemValues.Rows[0]["MachineOp"].ToString();
                    }
                    cmdSave.Text = "Save";
                }
                else
                {
                    cmdSave.Text = "Add";
                }
            }

        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {

            string sName = txtName.Text.Trim();
            string sDescription = txtDescription.Text.Trim();

            if (sName.Length == 0)
            {
                lblErrorMessage.Text = "ERROR: Name field cannot be empty.";
            }
            if (dropdownExecutionOrder.SelectedValue == "0")
            {
                lblErrorMessage.Text = "ERROR: You must pick a value for" + lblExecutionOrder.Text;
                return;
            }
            if (dropdownMachineOp.SelectedValue == "0")
            {
                lblErrorMessage.Text = "ERROR: You must pick a value for" + lblMachineOp.Text;
                return;
            }

            string sUserID = Request.LogonUserIdentity.Name;
            string sOp = Request.QueryString["Op"].ToString();
            int iId = -1;

            if (sOp == "Edit")
            {
                iId = int.Parse(Request.QueryString["ID"].ToString());

                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    objATBDB.UpdateDefTestSet(
                        iId, 
                        sName, 
                        int.Parse(dropdownExecutionOrder.SelectedValue), 
                        int.Parse(dropdownMachineOp.SelectedValue), 
                        sDescription, 
                        sUserID
                    );
                }
                Response.Redirect("TestSetHome.aspx");
            }
            else
            {
                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    iId = objATBDB.CreateDefTestSet(
                        sName,
                        int.Parse(dropdownExecutionOrder.SelectedValue),
                        int.Parse(dropdownMachineOp.SelectedValue),
                        sDescription,
                        sUserID
                    );
                }
                if (iId > 0)
                {
                    Response.Redirect("TestSetEditAssignments.aspx?ID=" + iId.ToString() + "&Name=" + Server.UrlEncode(sName));
                }
                else
                {
                    Response.Redirect("TestSetHome.aspx");
                }
            }
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("TestSetHome.aspx");
        }
    }
}